package com.numberone.web.controller.collect;

import com.numberone.framework.web.base.BaseController;
import com.numberone.system.domain.GoodsCollect;
import com.numberone.system.service.IGoodsCollectService;
import com.numberone.system.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/api/Collect")
public class GoodsCollectApiController extends BaseController {
    @Autowired
    private IGoodsCollectService goodsCollectService;

    /**
     * 获取用户商品收藏
     * @param userId
     * @return
     */
    @ResponseBody
    @GetMapping("/getGoodsCollect")
    public Object getGoodsCollect(Long userId, Integer status)
    {
        List<GoodsCollect> goodsCollects = null;
        if (status ==null)
        {
            GoodsCollect goodsCollect = new GoodsCollect();
            goodsCollect.setUserId(userId);
            goodsCollects = goodsCollectService.selectGoodsCollectList(goodsCollect);
            if (goodsCollects == null)
            {
                return JsonResult.nodata();
            }
        }else {
            goodsCollects = goodsCollectService.selectGoodsCollectListByStatus(userId, status);
            if (goodsCollects == null)
            {
                return JsonResult.nodata();
            }
        }
        return JsonResult.success(goodsCollects);
    }

    /**
     * 取消收藏
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/deleteGoodsCollect")
    public Object deleteGoodsCollect(Long id)
    {
        int i = goodsCollectService.deleteGoodsCollectById(id);
        if (i > 0)
        {
            return JsonResult.success();
        }
        return JsonResult.nodata();
    }

    /**
     * 收藏商品
     * @param goodsId
     * @param userId
     * @return
     */
    @ResponseBody
    @GetMapping("/addGoodsCollect")
    public Object addGoodsCollect(Long goodsId, Long userId)
    {
        GoodsCollect goodsCollect = new GoodsCollect();
        goodsCollect.setUserId(userId);
        goodsCollect.setGoodsId(goodsId);
        int i = goodsCollectService.insertGoodsCollect(goodsCollect);
        if (i > 0)
        {
            return JsonResult.success();
        }
        return JsonResult.repeat();
    }

    /**
     * 判断是否收藏
     * @param userId
     * @param goodsId
     * @return
     */
    @ResponseBody
    @GetMapping("/isCollect")
    public Object isCollect(Long userId, Long goodsId)
    {
        Boolean collect = goodsCollectService.isCollect(userId, goodsId);
        return JsonResult.success(collect);
    }

    /**
     * 搜索栏搜索
     * @param keyword
     * @return
     */
    @ResponseBody
    @GetMapping("/selectByKeyWord")
    public Object selectByKeyWord(Long userId, String keyword)
    {
        List<GoodsCollect> goodsCollects = goodsCollectService.selectByKeyWord(userId, keyword);
        if (goodsCollects != null)
        {
            return JsonResult.success(goodsCollects);
        }
        return JsonResult.nodata();
    }
}
